<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2017/11/6
  Time: 10:11
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>注册</title>
    <script type="text/javascript" src="${ctx }/static/js/jquery-1.10.1.min.js"></script>
</head>
<body style="background-image: url('${ctx}/static/img/bg/bg_login.jpg')">
<!-- 导航栏 -->
<jsp:include page="../../jsp/common/head.jsp"/>

<!-- 中部主体部分 -->
<div class="register">
    <br/><br/>
    <h1 style="text-align: center;color: aqua;"> R E G I S T E R </h1><br/>
    <center>
        <span id="message" style="color: red"></span><br/><br/>
        用&nbsp;&nbsp;户&nbsp;&nbsp;名 : <input type="text" id="username" name="username" placeholder=" 请输入用户名"/><br/><br/>
        密&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码 : <input type="password" id="password" name="password" placeholder=" 请输入密码"/><br/><br/>
        确认密码 : <input type="password" id="repassword" name="repassword" placeholder=" 再次输入密码"><br/><br/>
        邮&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;箱 : <input type="email" id="email" name="email" placeholder=" 请输入邮箱"/><br/><br/>
        <input type="button" id="register" name="register" value="注册"/>&nbsp;&nbsp;&nbsp;&nbsp;
        <input type="button" id="reset" name="reset" value="重置"><br/><br/>
        <a href="javascript:;" onclick="join_login_page()">返回登录 >></a>
    </center>
</div>

<!-- 底部 -->
<jsp:include page="../../jsp/common/foot.jsp"/>
</body>

<script type="text/javascript">
    $(function () {
        $("#register").click(function () {
            var username = $("#username").val();
            var password = $("#password").val();
            var repassword = $("#repassword").val();
            var email = $("#email").val();
            if (repassword == "") {
                $("#message").text("确认密码不能为空");
                return false;
            }
            if (email == "") {
                $("#message").text("邮箱不能为空");
                return false;
            }
            if (password != repassword) {
                $("#message").text("密码不匹配");
                return false;
            }
            $.ajax({
                type: "post",
                url: "${ctx}/user/register",
                contentType: "application/json",
                data: JSON.stringify({
                    date: new Date,
                    userName: username,
                    password: password,
                    email: email
                }),
                dataType: "json",
                success: function (result) {
                    if (result.status === 500) {
                        $("#message").text(result.msg);
                        return false;
                    }
                    if (result.status === 200) {
                        alert(result.msg);
                        window.location.href = "${ctx}/login.html"
                    }
                }
            })
        })

        $("input").change(function () {
            $("#message").text("");
        })

        $("#reset").click(function () {
            $("#username").val("");
            $("#password").val("");
            $("#repassword").val("");
            $("#email").val("");
        })
    })
</script>
</html>
